<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发布试卷</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<script src="/layui/layui.js"></script>
<script src="/js/core.util.js"></script>
<div class="user_table_div">
    <!--多条件查询-->
    <!--显示数据的表格-->
    <table class="layui-hide" id="user_table" lay-filter="user_table"></table>
    <!--分页相关 View-->
    <div id="laypage" class="layui-laypage-btn"></div>

    <!--赋予角色的 view-->
    <div id="roles" class="demo-transfer" style="display: none"></div>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <!--shiro:hasPermission="sys:user:delete"-->
        <button type="button" class="layui-btn layui-btn-danger" lay-event="batchDeleted">
            <i class="layui-icon"></i>批量发送
        </button>
    </div>
</script>
<script>
    layui.use(['jquery', 'form', 'table', 'laypage', 'layer', 'laydate', 'tree', 'transfer'], function () {

        const $ = layui.jquery;
        const form = layui.form;
        const table = layui.table;
        const laypage = layui.laypage
        const layer = layui.layer;
        const laydate = layui.laydate;
        const tree = layui.tree;

        /*分页参数*/
        const searchParam = {
            current: 1,
            size: 10,
            /*以下为多条件查询参数*/
            username: null,
            userId: null,
        }

        const examId = CoreUtil.getUrlParam("examId")


        /**
         *  加载分页数据
         */
        function loadPageInfo() {
            CoreUtil.sendAjax("/exam/user/page/student/exam", searchParam, function (res) {
                //初始化分页器
                laypageTable(res.total, searchParam.current);
                //初始化渲染数据
                loadTable(res.records);
                if (res.records != null) {
                    loadTable(res.records);
                }
            }, "GET", false, function (res) {
                //用户列表接口无权限回调
                layer.msg("抱歉！您暂无获取用户列表的权限");
                var noAuthorityData;
                noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        }


        //渲染分页插件
        const laypageTable = function (count, currentPage) {
            laypage.render({
                elem: 'laypage'
                , count: count
                , limit: searchParam.size
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , curr: location.hash.replace('#!currentPage=', '')     //获取起始页
                , hash: 'currentPage'                                   //自定义hash值
                , jump: function (obj, first) {
                    if (!first) {
                        searchParam.current = obj.curr;
                        searchParam.size = obj.limit;
                        loadPageInfo();
                    }
                }
            });
        };
        //渲染table
        const loadTable = function (data) {
            //展示已知数据
            table.render({
                elem: '#user_table'
                // 这个 data 为数组数据，直接 放到 data 上即可。
                , data: data
                , even: true
                , limit: data.length
                , limits: [10, 20, 30, 40, 50]
                , toolbar: '#toolbar'
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', width: 60, sort: true},
                        {field: 'username', title: '账号', width: 130},
                        {field: 'phoneNumber', title: '手机号', width: 130},
                        {field: 'nickname', title: '昵称', width: 130},
                        {field: 'mail', title: '邮箱', width: 150},
                        {
                            field: 'createTime', title: '创建时间', minWidth: 200, templet: function
                                (item) {
                                return CoreUtil.formattime(item.createTime);
                            }
                        },
                        {
                            field: 'updateTime', title: '更新时间', minWidth: 200, templet: function
                                (item) {
                                return CoreUtil.formattime(item.updateTime);
                            }
                        },
                    ]
                ]
            });
        };
        loadPageInfo();
        //点击新增事件， 表头工具栏事件
        table.on('toolbar(user_table)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg("请选择要批量发送的学生");
                    } else {
                        var userIds = [];
                        $(data).each(function (index, item) {
                            userIds.push(item.id);
                        });
                        tipDialog(userIds, "选中的");
                    }
                    break;
            }
            ;
        });

        //删除前确认对话框
        const tipDialog = function (userIds, username) {
            layer.open({
                content: '确定要发送么?',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax(`/exam/exam/send/${examId}`, JSON.stringify(userIds), function (res) {
                        layer.msg("试卷发送成功");
                        loadPageInfo();
                        $(".layui-laypage-btn").click();
                    }, "DELETE", false, function (res) {
                        //删除用户无权限
                        layer.msg("抱歉！您暂无删除用户的权限");
                    });
                }
            });
        };
    })
</script>

</body>
</html>